package com.ruoyi.web.course;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.TVideo;
import com.ruoyi.course.domain.TVideoDTO;
import com.ruoyi.course.service.ITVideoService;
import com.ruoyi.course.vo.SVideoVo;
import com.ruoyi.course.service.ISVideoService;
import com.ruoyi.course.vo.TVideoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生视频Controller
 *
 * @author ruoyi
 * @date 2024-12-08
 */
@Api(tags = "学生视频Controller")
@RestController
@RequestMapping("/course/s_video")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SVideoController extends BaseController {

    private final ISVideoService sVideoService;
    private final ITVideoService itVideoService;

    @ApiOperation("查询学生视频列表")
    @PreAuthorize("@ss.hasPermi('course:s_video:list')")
    @GetMapping("/list")
    public TableDataInfo<SVideoVo> list(SVideoVo entity) {
        return sVideoService.queryList(entity);
    }

    @ApiOperation("查询学生视频所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(SVideoVo entity) {
        return AjaxResult.success("查询成功", sVideoService.queryAll(entity));
    }

    @ApiOperation("导出学生视频列表")
    @PreAuthorize("@ss.hasPermi('course:s_video:export')")
    @Log(title = "学生视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SVideoVo entity) {
        List<SVideoVo> list = sVideoService.queryAll(entity);
        ExcelUtil<SVideoVo> util = new ExcelUtil<>(SVideoVo.class);
        util.exportExcel(response, list, "学生视频数据");
    }

    @ApiOperation("获取学生视频url")
    @PreAuthorize("@ss.hasPermi('course:s_video:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {

        //通过视频id查询视频url
        TVideoVo tVideoVo = itVideoService.queryById(id);
        String url = tVideoVo.getUrl();


        return AjaxResult.success("查询成功",url);

    }
    @ApiOperation("获取学生视频名称")
    @PreAuthorize("@ss.hasPermi('course:s_video:query')")
    @GetMapping(value = "/getVideoName/{id}")
    public AjaxResult getVideoName(@PathVariable("id") Long id) {

        //通过视频id查询视频名称
        TVideoVo tVideoVo = itVideoService.queryById(id);
        String name = tVideoVo.getName();
        return AjaxResult.success("查询成功",name);

    }
    @ApiOperation("查询视频列表所有名称")
    @GetMapping("/listAllName")
    public AjaxResult listAllName(TVideoVo entity) {
        LambdaQueryWrapper<TVideo> qw = new LambdaQueryWrapper<>();

        // 使用 select 方法，只查询 'name' 字段
        qw.select(TVideo::getName,TVideo::getUrl);  // 只查询 'name' 字段

        // 使用 MyBatis-Plus 提供的 queryAll 方法来查询符合条件的数据
        List<TVideo> videoList = itVideoService.list(qw);

        // 提取出查询结果中的所有名称
        List<TVideoDTO> videoNames = videoList.stream()
                .map(video -> {
                    TVideoDTO dto = new TVideoDTO();
                    dto.setName(video.getName());  // 设置视频名称
                    dto.setUrl(video.getUrl());    // 设置视频 URL
                    return dto;
                })
                .collect(Collectors.toList());

        // 返回查询结果
        return AjaxResult.success("查询成功", videoNames);
    }




}
